//
//  PublishNewViewController.m
//  Hui
//
//  Created by HawkingWang on 16/10/28.
//  Copyright © 2016年 Hui. All rights reserved.
//

#import "PublishNewViewController.h"
#import "LocationViewController.h"//地图定位Controller
#import "ConfirmNewPublishViewController.h"//确认派送订单Controller
#import "WebViewController.h"//网页Controller

@interface PublishNewViewController () <UITextFieldDelegate, UITextViewDelegate, UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

/**标题*/
@property (weak, nonatomic) IBOutlet UITextField *orderTitle;
/**收货类型*/
@property (weak, nonatomic) IBOutlet UILabel *style;
/**取货信息*/
@property (weak, nonatomic) IBOutlet UILabel *takeInfoLabel;
@property (weak, nonatomic) IBOutlet UILabel *takeName;
@property (weak, nonatomic) IBOutlet UILabel *takeAddress;

/**选择性别*/
@property (weak, nonatomic) IBOutlet UIImageView *maleImage;
@property (weak, nonatomic) IBOutlet UIImageView *femaleImage;
/**收货人姓名电话*/
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *phone;
/**地址*/
@property (weak, nonatomic) IBOutlet UILabel *address;
/**详细Label*/
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
/**详细地址*/
@property (weak, nonatomic) IBOutlet UITextView *detailAddress;
/**配送费*/
@property (weak, nonatomic) IBOutlet UITextField *money;

@end

@implementation PublishNewViewController
{
    NSString *_sex;
    BOOL _resign;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
//    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, 621);
    _scrollView.showsVerticalScrollIndicator = NO;
    _sex = @"0";
    _takeName.hidden = YES;
    _takeAddress.hidden = YES;
    _resign = NO;
    
    //键盘监听
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

/**选择派送类型*/
- (IBAction)chooseStyle:(id)sender {
}
/**取货信息*/
- (IBAction)takeInfo:(id)sender {
    [self resign];
    _takeInfoLabel.hidden = YES;
    _takeName.hidden = NO;
    _takeAddress.hidden = NO;
}
/**选择性别*/
- (IBAction)chooseMale:(id)sender {
    [self resign];
    _sex = @"0";
    _maleImage.image = [UIImage imageNamed:@"right-touch"];
    _femaleImage.image = [UIImage imageNamed:@"right"];
}
- (IBAction)chooseFemale:(id)sender {
    [self resign];
    _sex = @"1";
    _maleImage.image = [UIImage imageNamed:@"right"];
    _femaleImage.image = [UIImage imageNamed:@"right-touch"];
}
/**收货地址*/
- (IBAction)deliveryAddress:(id)sender {
    [self resign];
    LocationViewController *location = [[LocationViewController alloc] init];
    [self.navigationController pushViewController:location animated:YES];
}
/**➖*/
- (IBAction)minus:(id)sender {
    if ([_money.text integerValue] > 1) {
        _money.text = [NSString stringWithFormat:@"%ld", [_money.text integerValue] - 1];
    }
}
/**➕*/
- (IBAction)plus:(id)sender {
    _money.text = [NSString stringWithFormat:@"%ld", [_money.text integerValue] + 1];
}
/**派单说明*/
- (IBAction)introduction:(id)sender {
    WebViewController *web = [[WebViewController alloc] init];
    web.titleStr = @"派单说明";
    [self presentViewController:web animated:YES completion:nil];
}
- (IBAction)next:(id)sender {
    if ([_orderTitle.text isEqualToString:@""]) {
        [_orderTitle becomeFirstResponder];
        [EBUtility prompt:@"请输入派送标题" with:self];
//    }else if ([_style.text isEqualToString:@"请选择派送类型"]){
//        [EBUtility prompt:@"请选择派送类型" with:self];
    }else if (!_takeInfoLabel.hidden){
        [EBUtility prompt:@"请设置取货信息" with:self];
    }else if ([_phone.text isEqualToString:@""]){
        [_phone becomeFirstResponder];
        [EBUtility prompt:@"请输入收货人电话" with:self];
//    }else if ([_address.text isEqualToString:@"小区/写字楼/学校等"]){
//        [EBUtility prompt:@"请设置收货地址" with:self];
//    }else if (!_detailLabel.hidden){
//        [EBUtility prompt:@"请填写详细地址" with:self];
    }else{
        [self resign];
        ConfirmNewPublishViewController *confirm = [[ConfirmNewPublishViewController alloc] init];
//        confirm.styleStr = _style.text;
        confirm.styleStr = @"外卖";
        confirm.orderTitleStr = _orderTitle.text;
        confirm.takeNameStr = _takeName.text;
        confirm.takeAddressStr = _takeAddress.text;
        if ([_sex isEqualToString:@"0"]) {
            confirm.deliveryNameStr = [NSString stringWithFormat:@"%@先生 %@", _name.text, _phone.text];
        }else{
            confirm.deliveryNameStr = [NSString stringWithFormat:@"%@女士 %@", _name.text, _phone.text];
        }
        confirm.deliveryAddressStr = _detailAddress.text;
        confirm.moneyStr = [NSString stringWithFormat:@"%@.0", _money.text];
        [self.navigationController pushViewController:confirm animated:YES];
    }
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    [self resign];
    [_orderTitle resignFirstResponder];
    [_name resignFirstResponder];
    [_phone resignFirstResponder];
    if (_resign) {
        [_detailAddress resignFirstResponder];
        [_money resignFirstResponder];
        _resign = NO;
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    if ([_detailAddress.text isEqualToString:@""]) {
        _detailLabel.hidden = NO;
    }
}
- (void)textViewDidBeginEditing:(UITextView *)textView{
    _detailLabel.hidden = YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self resign];
}
- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)resign{
    [_orderTitle resignFirstResponder];
    [_name resignFirstResponder];
    [_phone resignFirstResponder];
    [_detailAddress resignFirstResponder];
    [_money resignFirstResponder];
}
#pragma mark 键盘显示的监听方法
-(void) keyboardWillShow:(NSNotification *) note
{
    if ([_detailAddress isFirstResponder] || [_money isFirstResponder]) {
        // 获取键盘的位置和大小
        CGRect keyboardBounds;
        [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
        NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
        NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
        
        // Need to translate the bounds to account for rotation.
        keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];
        
        // 获取输入框的位置和大小
        CGRect containerFrame;
        CGFloat height;
        CGFloat orginY;
        if ([_detailAddress isFirstResponder]) {
            containerFrame = _detailAddress.frame;
            height = 84;
            orginY = 449;
        }else if ([_money isFirstResponder]){
            containerFrame = _money.frame;
            height = 60;
            orginY = 542;
        }
        /// 动画改变位置
        [UIView animateWithDuration:[duration doubleValue] animations:^{
            [UIView setAnimationBeginsFromCurrentState:YES];
            [UIView setAnimationDuration:[duration doubleValue]];
            [UIView setAnimationCurve:[curve intValue]];
            // 更改输入框的位置
            CGFloat y;
            if ([_detailAddress isFirstResponder]) {
                if (SCREEN_WIDTH == 375) {
                    y = 714 + (keyboardBounds.size.height - 176) - (SCREEN_HEIGHT - 64);
                }else if (SCREEN_WIDTH == 414){
                    y = 714 + (290 - 176) - (SCREEN_HEIGHT - 64);
                }else{
                    y = 714 + (keyboardBounds.size.height - 176) - (SCREEN_HEIGHT - 64);
                }
            }else if ([_money isFirstResponder]){
                if (SCREEN_WIDTH == 375) {
                    y = 714 + (keyboardBounds.size.height - 106) - (SCREEN_HEIGHT - 64);
                }else if (SCREEN_WIDTH == 414){
                    y = 714 + (290 - 106) - (SCREEN_HEIGHT - 64);
                }else{
                    y = 714 + (keyboardBounds.size.height - 106) - (SCREEN_HEIGHT - 64);
                }
            }
            [_scrollView setContentOffset:CGPointMake(0, y) animated:NO];
            _resign = YES;
        }];
    }
}

#pragma mark === 键盘隐藏的监听方法 ===
-(void) keyboardWillHide:(NSNotification *) note
{
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    
    // 动画改变位置
    [UIView animateWithDuration:[duration doubleValue] animations:^{
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:[duration doubleValue]];
        [UIView setAnimationCurve:[curve intValue]];
        // 更改输入框的位置
        _scrollView.contentOffset = CGPointMake(0, 714 - SCREEN_HEIGHT - 64);
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
